<?php

/***
 * 描述：商铺商品库
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id: ProductLibModel.class.php 39 2013-10-28 11:30:40Z whtaok@gmail.com $
 */
class ProductLibModel extends CommonModel {
	protected $_auto = array (
			array('is_valid','2',1), 	// 系统默认为2，审核通过
			array('is_show','1',1), 	// 系统默认为1，显示
			array('crt_time','time',1,'function'), // 对crt_time字段在更新的时候写入当前时间戳
			array('upd_time','time',3,'function'), // 对upd_time字段在更新的时候写入当前时间戳
			array('crt_user','getSUID',1,'callback'), // 对name字段在新增的时候回调getSUID方法
			array('upd_user','getSUID',3,'callback'), // 对name字段在新增的时候回调getSUID方法
			
					
	);
	protected $_validate = array(
			array('barcode','require','条形码已经存在！',0,'unique',3),
			array('name','require','商品名称不能为空！',0),
			array('price_buy','require','进货价不能为空！',0),
			array('price_sale','require','零售价不能为空！',0),
			array('price_vip','require','会员价不能为空！',0),
			array('cate_code1','require','分类1不能为空！',0),
			array('cate_code2','require','分类2不能为空！',0),
			array('spec_qty','require','每组的数量不能为空！',0),
			array('spec_qty','number','数量格式不正确！',0),
	);
	
	function findListPage($search,$order='ordlist'){
		if($search['cate_code']){
			$modWhere['cate_code2'] = array('like',I('cate_code').'%');
		}
		if($search['name']){
			$modWhere['name'] = array('like',I('name').'%');
		}
		if($search['barcode']){
			$modWhere['barcode'] = array('like','%'.I('barcode').'%');
		}
		$totalRows = $this->setModWhere($modWhere)->count();
		$rtnData = $this->setModWhere($modWhere)->listPage($totalRows);
		return $rtnData;
	}
	
	/**
	 * 添加单件商品
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	function addOne(){
		$cateCode = I('cate_code');
		$cateCode1 = get_productcate_code1($cateCode);
		$cateCode2 = $cateCode;
		$data = array(
				'barcode'=>uniqid(),
				'brand'=>I('brand'),
				'name'=>I('name'),
				'spec'=>I('spec'),
				'spec_qty'=>I('spec_qty'),
				'spec_unit'=>I('spec_unit'),
				'spec_qty'=>I('spec_qty','1',''),
				'price_buy'=>I('price_buy'),
				'price_sale'=>I('price_sale'),
				'price_vip'=>I('price_vip'),
				'cate_code1'=>$cateCode1,
				'cate_code2'=>$cateCode2,
				'stock_prod_id'=>I('stock_prod_id','0',''),
		);
		if($this->create($data)){
			$lastId = $this->add();
			return $lastId;
		}else{
			return false;
		}
	}
	
	/**
	 * 编辑单件商品
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	function updOne(){
		$cateCode = I('cate_code');
		$cateCode1 = get_productcate_code1($cateCode);
		$cateCode2 = $cateCode;
		$data = array(
				'id'=>I('id'),
				'brand'=>I('brand'),
				'name'=>I('name'),
				'spec'=>I('spec'),
				'spec_unit'=>I('spec_unit'),
				'spec_qty'=>I('spec_qty','1',''),
				'price_buy'=>I('price_buy'),
				'price_sale'=>I('price_sale'),
				'price_vip'=>I('price_vip'),
		);
		if($this->create($data)){
			$flag = $this->save();
			return $flag;
		}else{
			return false;
		}
	}
	
}

?>